﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

namespace LS.Screening.Core
{
    /// <summary>
    /// 异常上下文扩展类
    /// </summary>
    public static class ExceptionContextExtension
    {
        /// <summary>
        /// 生成异常日志
        /// </summary>
        /// <param name="context"></param>
        /// <param name="appType"></param>
        /// <param name="appName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static ExceptionLog AddLog(this ExceptionContext context, string appName, object parameters)
        {
            ExceptionLog exceptionLog = new ExceptionLog
            {
                AppType = AppType.WebAPI,
                AppName = appName,
                LogLevel = LogLevel.Error,
                ServiceName = context.ActionDescriptor.RouteValues["controller"],
                MethodName = context.ActionDescriptor.RouteValues["action"],
                Parameters = parameters,
                Remark = null,
                Tags = "Exception",
                ErrorMessage = context.Exception.GetErrorMessage(),
                StackTrace = context.Exception.StackTrace
            };
            return exceptionLog;
        }
    }
}
